package com.yager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xpath.internal.operations.Or;
import com.yager.mapper.OrderMapper;
import com.yager.model.Order;
import com.yager.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Component
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;


    @Override
    public void publishNewOrder(String userid,String houseid,String orderdescription) {

        String max=orderMapper.findMaxId();
        max=max.substring(1);

        Integer i=Integer.parseInt(max);
        i++;
        String id="O";

        if(i<10) id=id+"00"+i.toString();
        else if(i<100) id=id+"0"+i.toString();
        else id=id+i.toString();

        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String datetime=sdf.format(new Date());
        Date date= null;
        try {
            date = sdf.parse(datetime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Order order=new Order(id,userid,houseid,orderdescription,true,date);

        order.setOrderid(id);

        orderMapper.insert(order);
        System.out.println("添加新招募完成，招募id为："+order.getOrderid());
    }

    @Override
    public void changeOrderInfo(Order order) {

        orderMapper.updateByPrimaryKey(order);
        System.out.println("招募更新已完成，当前招募信息为"+order);

    }

    @Override
    public List<Order> getAllOrders() {
        List<Order> list=orderMapper.getAllOrders();
        System.out.println("查询全部招募，数目为："+list.size());
        return list;
    }

    @Override
    public PageInfo<Order> getOrdersByUserId(String id,Integer pageNo) {
        PageHelper.startPage(pageNo,1);
        List<Order> list=orderMapper.getOrdersByUserId(id);
        PageInfo<Order> orderPageInfo = new PageInfo<>(list,5);
//        System.out.println("已查询userid为"+id+"的用户发布的所有招募");
        return orderPageInfo;
    }

    @Override
    public PageInfo<Order> getOrdersByHouseId(String id, Integer pageNo) {
        PageHelper.startPage(pageNo, 1);
        List<Order> list=orderMapper.getOrdersByHouseId(id);
        PageInfo<Order> orderPageInfo = new PageInfo<>(list, 3);
//        for(int i: orderPageInfo.getNavigatepageNums()){
//            System.out.println(i);
//        }
//        System.out.println("service中pageinfo的数量为："+orderPageInfo.getNavigatepageNums());
        return orderPageInfo;
    }

    @Override
    public Order getOrderById(String id) {
        Order order=orderMapper.selectByPrimaryKey(id);
        System.out.println("id为"+id+"的招募信息："+order);
        return order;
    }

    @Override
    public void deleteOrderById(String id) {
        orderMapper.deleteByPrimaryKey(id);
        System.out.println("已经删除id为"+id+"的招募（或者没有，但至少运行过了");
    }

    @Override
    public void changeOrderStatus(String id) {
        Order order=orderMapper.selectByPrimaryKey(id);
        if(order.getOrderstatus()){
            order.setOrderstatus(false);
        }
        else{
            order.setOrderstatus(true);
        }
        orderMapper.updateByPrimaryKey(order);
    }

    @Override
    public void addUserToOrder(String userid, String orderid) {
        Order order=orderMapper.selectByPrimaryKey(orderid);
        if(order.getApplyusers()==null){
            order.setApplyusers(userid);
        }
        else{
            String users=order.getApplyusers()+","+userid;
        }
        orderMapper.updateByPrimaryKey(order);
    }

    @Override
    public boolean isHouseHasOrder(String houseid){
        int count=orderMapper.getHouseOrderCount(houseid);
        if(count>0) return true;
        else return false;
    }

    @Override
    public boolean isUserHasOrder(String userid){
        int count=orderMapper.getUserOrderCount(userid);
        if(count>0) return true;
        else return false;
    }

    @Override
    public void removeOrderByHouseId(String houseid) {
        orderMapper.deleteOrderByHouseId(houseid);
    }
}
